// pages/credentials/credentialsEnroll/credentialsEnroll.js
import {
  getAuthCode
} from '../../../api/user';
import {
  applyCertificate
} from '../../../api/certificate';
import nations from '../../../resource/data/nations.js';
import {
  getSubscribeMessageAuth
} from '../../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    certificateId: 0,
    name: '',
    sexType: 1, // 1-男 2-女
    birthday: '',
    nation: '',
    nationOptions: nations,
    politics: '',
    politicsOptions: ['群众', '中共党员', '中共预备党员', '共青团员', '其他'],
    idCard: '',
    education: '',
    educationOptions: ['初中及以下', '中专/中技', '高中', '大专', '本科', '硕士', '博士'],
    graduation: '',
    major: '',
    register: '',
    address: '',
    mobile: '',
    authCode: '',
    authBtnText: '获取验证码',
    isSend: false,
    countdown: 60,
    checked: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.certificateId = Number(options.id);
  },

  /**
   * 提交审核
   */
  handleCommit() {
    if (this.data.name === '') {
      this.showToast('请输入姓名');
    } else if (this.data.birthday === '') {
      this.showToast('请选择出生年月');
    } else if (this.data.nation === '') {
      this.showToast('请选择民族');
    } else if (this.data.politics === '') {
      this.showToast('请选择政治面貌');
    } else if (!(/(^\d{15}$)|(^\d{17}([0-9]|X|x)$)/.test(this.data.idCard))) {
      this.showToast('请输入正确格式的身份证号码');
    } else if (this.data.education === '') {
      this.showToast('请选择学历');
    } else if (this.data.graduation === '') {
      this.showToast('请输入毕业院校');
    } else if (this.data.major === '') {
      this.showToast('请输入专业');
    } else if (this.data.register === '') {
      this.showToast('请输入户籍地址');
    } else if (this.data.address === '') {
      this.showToast('请输入现居地址');
    } else if (!/^1[3|4|5|6|8|7|9][0-9]\d{8}$/.test(this.data.mobile)) {
      this.showToast('请输入正确的手机号');
    } else if (this.data.authCode === '') {
      this.showToast('请输入验证码');
    } else if (!this.data.checked) {
      this.showToast('请阅读并同意 自愿声明');
    } else {
      getSubscribeMessageAuth(['u2ia6xpt2JcK-JH9GdHlI10IjCoisDXfOJI872vajuk']).then(() => {
        return applyCertificate({
          address: this.data.address,
          birthday: this.data.birthday + '-01',
          certificateId: this.data.certificateId,
          education: this.data.education,
          graduation: this.data.graduation,
          idCard: this.data.idCard,
          major: this.data.major,
          mobile: this.data.mobile,
          name: this.data.name,
          nation: this.data.nation,
          politics: this.data.politics,
          register: this.data.register,
          sex: this.data.sexType,
        });
      }).then(() => {
        this.showToast('提交成功');

        // 修改上一个页面的状态
        const pages = getCurrentPages();
        const prevPage = pages[pages.length - 2];
        prevPage.data.intro.status = 0;

        let timer = setTimeout(() => {
          clearTimeout(timer);
          wx.navigateBack({});
        }, 2000)
      })
    }
  },

  /**
   * 发送验证码
   */
  handleSendAuthCode() {
    if (!/^1[3|4|5|6|8|7|9][0-9]\d{8}$/.test(this.data.mobile)) {
      this.showToast('请输入正确的手机号');
    } else {
      getAuthCode(this.data.mobile);
      this.countDown();
    }
  },

  /**
   * 倒计时
   */
  countDown() {
    if (this.data.countdown === 0) {
      this.setData({
        isSend: false,
        authBtnText: '重新获取',
        countdown: 60,
      })
      return
    } else {
      this.setData({
        isSend: true,
        authBtnText: `${this.data.countdown--}s`
      })
    }
    let timer = setTimeout(() => {
      clearTimeout(timer);
      this.countDown()
    }, 1000)
  },

  /**
   * 姓名
   */
  onInputName(e) {
    this.setData({
      name: e.detail.value,
    })
  },

  /**
   * 性别
   * @param {*} e 
   */
  handleChangeSexType(e) {
    this.setData({
      sexType: Number(e.currentTarget.dataset.type)
    })
  },

  /**
   * 出生年月
   */
  onChangeTime(e) {
    this.setData({
      birthday: e.detail.value
    });
  },

  /**
   * 民族
   */
  onChangeNation(e) {
    this.setData({
      nation: this.data.nationOptions[Number(e.detail.value)]
    });
  },

  /**
   * 政治面貌
   */
  onChangePolitics(e) {
    this.setData({
      politics: this.data.politicsOptions[Number(e.detail.value)]
    });
  },

  /**
   * 学历
   */
  onChangeEducation(e) {
    this.setData({
      education: this.data.educationOptions[Number(e.detail.value)]
    });
  },

  /**
   * 身份证
   * @param {object} e 
   */
  onInputIDCard(e) {
    this.data.idCard = e.detail.value.trim();
  },

  /**
   * 毕业院校
   * @param {object} e 
   */
  onInputGraduation(e) {
    this.data.graduation = e.detail.value.trim();
  },

  /**
   * 专业
   * @param {object} e 
   */
  onInputMajor(e) {
    this.data.major = e.detail.value.trim();
  },

  /**
   * 户籍地址
   * @param {object} e 
   */
  onInputRegister(e) {
    this.data.register = e.detail.value.trim();
  },

  /**
   * 居住地址
   * @param {object} e 
   */
  onInputAddress(e) {
    this.data.address = e.detail.value.trim();
  },

  /**
   * 手机号
   * @param {object} e 
   */
  onInputMobile(e) {
    this.data.mobile = e.detail.value.trim();
  },

  /**
   * 验证码
   * @param {object} e 
   */
  onInputAuthCode(e) {
    this.data.authCode = e.detail.value.trim();
  },

  /**
   * 同意自愿声明
   */
  handleAgree(e) {
    this.setData({
      checked: e.detail.value[0] === '1'
    })
  },

  /**
   * 吐司
   * @param {string} msg 
   */
  showToast(msg) {
    wx.showToast({
      title: msg,
      icon: 'none'
    })
  },

})